$(function() {
	$(".chongfubtn").click(function() {
		$('#djForm')[0].reset();
		$("#abcdefg").hide();
		if(infoTable){
			infoData.needId=$("#chongfuId").val();
			infoTable.draw();
		}
		opend();
	});
});
function initDuibiData(id){
	$.ajax({
		url:rootPath+"/ui/eventReport/findById/"+id,
		dataType:'json',
		type:'get',
		success:function(r){
			var data=r.data.info;
			console.log(data);
			//填充对比事件信息
			$("#djTime").val(
					moment(data.reportAccTime).format('YYYY/MM/DD HH:mm'));

			if (data.reportUser) {
				$("#djUser").val(data.reportUser.name);
				$("#djPhone").val(data.reportUser.contactType);
			}

			$("#djImg").attr("src", rootPath + "/" + data.reportImgAdress);

			$("#djAdress").val(data.reportAdress);

			if (data.reportEventToNetGrid)
				$("#djGrid").val(data.reportEventToNetGrid.gridName);

			
			if (data.reportEventType)
				$("#djType").val(data.reportEventType.eventName);
		}
	});
	$("#abcdefg").show();
};
/*$(document).on("click",".duibiBtn",function(){
	var id=$(this).attr("defineId");
	
	//$("#djDiv").show();
});*/
var infoTable;
var infoData = {};

function initInfoTable(reportId){
	infoData.needId=reportId;
	//var url=rootPath + "/ui/eventReport/findInitedInfo/" + reportId;
	var url=rootPath + "/ui/eventReport/findInitedInfo";
	infoTable = $('#infoTable').DataTable({
		"serverSide" : true,
		"ordering" : false,
		"ajax" : {
			"dataType" : "json",
			"type" : "get",
			"cache" : false,
			"url" : url,
			"data" : function(d) {
				return $.extend({}, d, infoData);
			}
		},
		columns : [
				{
					data : null,
					defaultContent : ""
				},
				
				{
					data : 'reportUser.name'
				},
				
				{
					data : 'reportAdress',
					render : function(data, type, full, meta) {
						if (data == null || data == undefined
								|| data == "") {
							return data;
						} else {
							return (data.length > 10) ? '<font title="'
									+ data
									+ '">'
									+ data.substring(0, 10)
									+ '...' + '</font>'
									: data;
						}
					}
				},
				{
					data : 'reportAccTime',
					render : function(data, type, full, meta) {
						if (data)
							return moment(data).format(
									'YYYY/MM/DD HH:mm');
						else
							return '';
					}
				},
				{
					data : null,
					defaultContent : "",
					render : function(data, type, full, meta) {
						var operate = '<button type="button" class="btn btn-sm btn-info duibiBtn" title="处理">对比</button>';
						
						return operate;
					}
				}
		],
		"drawCallback" : function(oSettings) {
			var k = 0;
			var total = oSettings.aiDisplay.length
					+ oSettings._iDisplayStart;
			for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
				$(
						'td:eq(0)',
						oSettings.aoData[oSettings.aiDisplay[k]].nTr)
						.text(i + 1);
				k++;
			}
			infoTable.off('click', 'tr');
			infoTable.on('click','tr',function(event) {
			event = event || window.event;
			var target = event.target||event.scrElement;
			var data = infoTable.row(this).data();
			if (target.className.indexOf('duibiBtn') != -1) {
				initDuibiData(data.id);
			}
			
		
			return;
		});
		}
	});
};
function opend() {
	//$("#infoTbody").empty();
	var reportId = $("#chongfuId").val();
	if (reportId == '' || reportId == null || reportId == undefined) {
		alert("请选择上报的事件源");
		return false;
	}
	initInfoTable(reportId);
	/*$.ajax({
		url : rootPath + "/ui/eventReport/findInitedInfo/" + reportId,
		dataType : 'json',
		type : 'get',
		success : function(r) {
			var list = r.data.list;
			if (list.length > 0) {
				$.each(list, function() {
					var tr = $("<tr class='trClick'></tr>");
					
					// var td1 = $("<td>" + this.reportEventType.eventName +
					// "</td>");
					// var td2 = $("<td>" + this.reportContent + "</td>");
					var td3 = $("<td>" + this.reportUser.name + "</td>");
					var td4 = $("<td>" + this.reportAdress + "</td>");
					// var td5 = $("<td>" + this.longitude + "</td>");
					// var td6 = $("<td>" + this.latitude + "</td>");
					var timeTd = $("<td>"
							+ moment(this.reportAccTime).format(
									"YYYY/MM/DD HH:mm") + "</td>");
					var td7 = $("<td><button defineId='"+this.id+"' class='btn btn-info btn-sm duibiBtn'>对比</button></td>");
					$("#infoTbody").append(tr)// .append(td1).append(td2)//
					.append(td3).append(td4)// .append(td5).append(td6)//
					.append(timeTd).append(td7);
				});
			}
			$("#chongfuModal").modal({
				backdrop : 'static'
			});
		}
	});*/
	$("#chongfuModal").modal({
		backdrop : 'static'
	});
};